home *** CD-ROM | disk | FTP | other *** search
- // copying
- // Copyright (C) 1991 David Stoutamire (daves@alpha.ces.cwru.edu)
- //
- // This program is free software; you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, version 2.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program (file "COPYING"); if not, write to the Free
- // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
- // includes
- #include <stream.h>
- #include <ctype.h>
- #include <std.h>
- #include <String.h>
- #include <File.h>
- #include <sys/time.h>
- #include <sys/resource.h>
- #ifndef IRIX
- #include <CursesW.h>
- #else /* IRIX */
- #ifdef IRIX
- #define USG 1
- #endif
- #endif /* IRIX */
- #include <curses.h>
- #ifdef IRIX
- #include <CursesW.h>
- #undef USG
- #endif /* IRIX */
-
- // enums and consts
- enum bool { false, true };
- enum boardenum { black, white, empty };
- enum stoneenum { blackstone, whitestone };
- enum moveenum { play, pass, resign, invalid };
- enum territoryenum { seki, dame, alivewhite, aliveblack,
- deadwhite, deadblack,
- territorywhite, territoryblack };
- enum resultenum { blacklost, whitelost, blackresigned, whiteresigned,
- unfinished, tied, unknown };
- enum playerenum { randomopponent, cursesopponent, mapopponent };
- enum patenum { patnxn, patdiamond, patgroup };
-
- const float reallylowfloat = -999999.0;
-
- // class protos
- class Pos;
- class Board;
- class Game;
- class Move;
- class Opponent;
- class PatternusingOpponent;
- class Tactician;
- class HashOpponent;
- class SDCOpponent;
- class RandomOpponent;
- class CursesOpponent;
- class MapOpponent;
- class GreedyOpponent;
- class MetaOpponent;
- class String;
- class MLCG;
- class Pattern;
- class PatRep;
- class Patnxn;
- class Patdiamond;
- class Patgroup;
-
- // typedefs, defines, globals, misc.
- typedef void (*movefuncptr)(Move); // typedefs for functions with function args
- typedef void (*Stringfuncptr)(String&);
-
- #ifdef MAINFILE
- #define INIT(x) =x
- #define GLOBAL
- #else
- #define INIT(x)
- #define GLOBAL extern
- #endif
-
- GLOBAL bool errortest INIT(true); // Do error checking?
- GLOBAL bool shouldabort INIT(false); // Blow up on a fatal?
- GLOBAL bool allowsuicide INIT(true); // Allow suicide moves as legal?
- GLOBAL bool considerresignation INIT(false); // Even consider resigning?
- GLOBAL bool dontprint INIT(false); // print out score when learning?
- GLOBAL bool dolearn INIT(true); // learn during study?
-
- GLOBAL String evalfile INIT("nofile");
- GLOBAL String dribble INIT("nofile");
- GLOBAL String postscriptdir INIT("nodir");
-
- GLOBAL int global_argc; // allows constructors to read arg list
- GLOBAL char **global_argv; // (set in main)
-
- // This may have to change at your site.
- // This is set up to work with the supplied games if run in the
- // compiling directory.
- GLOBAL String allgames INIT("games/*/*");
-
- #define chkpt(x) cout << form("chkpt at %c.\n",x)
- #define loop for(;;)
- #define doboard(i,j) for(i=0;i<19;i++)for(j=0;j<19;j++)
- #define onboard(i,j) ((i<19)&&(j<19)&&(i>=0)&&(j>=0))
- #define PatternHASH(x) (x.hash())
- #define foreach(m,s) for(m=s.first();m!=0;s.next(m))
-
- // function prototypes
- volatile void fatal(String errmsg);
- Opponent *makeopponent();
- const int syma(int a,int b,int i);
- const int symb(int a,int b,int i);
- void Patgroup_groupify();
- String Patgroup_gex(int iter);
- String nextarg();
- void playgame();
- void study();
- void getnum(int& what);
- void takescore(int i, float f, int compress=1);
-
- class Pos {
- public:
- int row, col; // 0--18, 0--18
- Pos(); // constructor for invalid position
- Pos(String s); // construct from Korshelt notation
- Pos(int r, int c); // construct from numbers (row, col)
- bool valid(); // is it a valid position?
- bool operator== (Pos vs);
- bool operator!= (Pos vs);
- Pos up(); // these may return 'invalid'.
- Pos down();
- Pos left();
- Pos right();
- String str(); // move in Korschelt notation
- };
- #include "Pos.SLSet.h"
-
- class Move {
- public:
- moveenum kind;
- Pos where; // only matters for type 'play'
- Move();
- // Move(Move& m);
- Move(moveenum k, Pos at=Pos());
- Move(String s); // contruct from a string
- bool valid(); // is it not 'invalid'?
- // void operator= (Move m);
- bool operator== (Move vs);
- const String str(); // make a string out of move
- };
-
- class Block {
- // an adjacent bunch of stones of the same color
- public:
- PosSLSet stones;
- PosSLSet liberties;
- stoneenum color;
- Block();
- Block(Block& b);
- Block(Board& b, Pos p);
- String str();
- };
- #include "Block.DLList.h"
-
- class Board {
- void removeblock(Pix bl); // utility for deleting blocks
- void transfer(Pix from, Pix to); // merge and delete from into to
- public:
- boardenum board[19][19]; // what the board looks like
- Pix blocks[19][19]; // fast access by position
- BlockDLList allBlocks; // lists of blocks
- int movenum; // move number (after handicap)
- Pos korestriction; // if valid, play forbidden here
- bool gameover; // has the game terminated?
- String comment; // any old comment
- stoneenum turntoplay; // whose move it is
- int whitetaken, blacktaken; // captured stones
-
- Board(String hand=""); // make Board for beginning of game
- Board(Board& b); // copier
- void operator=(Board& b); // assignment
-
- void applymove(Move m); // destructively apply a move
- bool islegalmove(Move m); // is a move legal for this board?
-
- String str(); // string representation of board
- int sizeat(Pos p); // size of group at p
- int libsat(Pos p); // # libs of group at p
- };
-
- class Game {
- private:
- float komi;
- String handicap;
- resultenum result; // result of game: who won, etc.
- float finalscore; // score() returns finalscore - komi
- const int Game_maxmoves=400;
- Board current; // cached way board looks
- int currentidx; // idx of current;
- public:
- int nummoves;
- Board& snapshot(int idx); // how board is before move idx
- Move moves[Game_maxmoves];
- String comment;
- Game(String filename);
- float score(); // w/ respect to black, includes komi
- void save(String filename);
- String str();
- };
-
- class PatRep {
- public:
- virtual PatRep* duplicate();
- virtual patenum identification();
- virtual void extract(PatternusingOpponent& op,
- Board& b, Move m);
- virtual bool operator== (PatRep& vs);
- virtual unsigned int hash();
- virtual String str();
- virtual ~PatRep() {};
- };
-
- class Pattern {
- // Pattern is really a handle for PatRep to allow virtual calls
- private:
- PatRep* rep;
- public:
- Pattern(patenum patterntouse=patnxn, String s=""); // provided for loading
- Pattern(Pattern& p);
- ~Pattern();
- void extract(PatternusingOpponent& op,
- Board& b, Move m);
- bool operator== (Pattern& vs);
- void operator= (Pattern& p);
- unsigned int hash(int hashsize=0);
- String str(); // for saving
- };
- #include "Pattern.float.CHMap.h"
-
- class Patnxn: public PatRep {
- private:
- String rep;
- public:
- Patnxn(String s="");
- Patnxn(PatRep& p);
- PatRep* duplicate();
- patenum identification();
- void extract(PatternusingOpponent& op,
- Board& b, Move m);
- bool operator== (PatRep& vs);
- unsigned int hash();
- String str();
- };
-
- class Patdiamond: public PatRep {
- private:
- String rep;
- public:
- Patdiamond(String s="");
- Patdiamond(PatRep& p);
- PatRep* duplicate();
- patenum identification();
- void extract(PatternusingOpponent& op,
- Board& b, Move m);
- bool operator== (PatRep& vs);
- unsigned int hash();
- String str();
- };
-
- class Patgroup: public PatRep {
- private:
- String rep;
- public:
- Patgroup(String s="");
- Patgroup(PatRep& p);
- PatRep* duplicate();
- patenum identification();
- void extract(PatternusingOpponent& op,
- Board& b, Move m);
- bool operator== (PatRep& vs);
- unsigned int hash();
- String str();
- };
-
- class Opponent {
- public:
- String filename; // disk image - "nofile" if none
- float playevals[19][19];
- bool playable[19][19];
- Move getmove(Board& b);
- virtual float evalpos(Board& b, Pos p);
- virtual void evaluate(Board& b);
- virtual void docheckpt(); // sync disk and memory
- virtual void study(Board& b,
- Move m); // study a particular move
- // the way to handle studying, by default
- virtual void slide(Board& b, Pos p, float deriv);
- float score(Move m); // print normalized error
- virtual ~Opponent();
- };
-
- class PatternusingOpponent: public Opponent {
- protected:
- Pattern extract(Board& b,Move m); // extract pattern with my specs
- public:
- patenum patterntouse; // which type of pattern
- int nxnsize; // used by Patnxn
- int mindiamondsize, maxdiamondsize, // used by PatDiamond
- diamondmustsee, libscutoff; // used by PatGroup
- int groupradius;
- PatternusingOpponent();
- };
-
- class HashOpponent: public PatternusingOpponent {
- private:
- float *table;
- int hashsize;
- public:
- HashOpponent();
- ~HashOpponent();
- void docheckpt();
- float evalpos(Board& b, Pos p);
- void slide(Board& b, Pos p, float deriv);
- };
-
- class MapOpponent: public PatternusingOpponent {
- private:
- PatternfloatCHMap vals;
- public:
- MapOpponent();
- String description();
- float evalpos(Board& b, Pos p);
- void evaluate(Board& b, Move m);
- void slide(Board& b, Pos p, float deriv);
- void docheckpt();
- friend void learn(Board& b, Move m);
- };
-
- class MetaOpponent: public Opponent {
- private:
- Opponent **agents;
- int numagents;
- public:
- MetaOpponent();
- ~MetaOpponent();
- void evaluate(Board& b);
- void study(Board& b, Move m);
- };
-
- class RandomOpponent: public Opponent {
- public:
- float evalpos(Board& b, Pos p);
- };
-
- class GreedyOpponent: public Opponent {
- public:
- float evalpos(Board& b, Pos p);
- };
-
- class CursesOpponent: public Opponent {
- private:
- int row=9, col=9;
- CursesWindow *w;
- public:
- CursesOpponent();
- ~CursesOpponent();
- void evaluate(Board& b);
- };
-